<%@page pageEncoding="UTF-8" import="ajaxjs.admin.PageEditor.*"%>
<%@taglib prefix="commonTag" 	tagdir="/WEB-INF/tags/common/html"%>
<%@taglib prefix="commonUI" 	tagdir="/WEB-INF/tags/common/UI"%>
<%@taglib prefix="prototypeUI"	tagdir="/WEB-INF/tags/common/Prototype"%>   	
<%
	PageEditor pageEditor = new PageEditor();
	// 保存数据
	if(request.getMethod().equals("POST")){
		if(request.getParameter("action") != null 
		&& request.getParameter("action").equals("upload") 
		&& request.getParameter("folder") != null){
			// 上传图片
				out.write(pageEditor.uploadFile(request));
			// 保存数据	
		} else 
				out.write(PageEditor.save_jsp_fileContent(request));
		return;
	}
	
	// 删除图片
	if(request.getMethod().equals("DELETE") && request.getParameter("file") != null){
		out.write(pageEditor.delFile(request));
		return;
	}
	
	if(request.getParameter("action") != null){
		if(request.getParameter("action").equals("getImgList") && request.getParameter("folder") != null){
			// 获取图片列表
			out.write(pageEditor.getImgList(request));
			return;
		}
	}
	
	pageEditor.getIframe_basePath(request);
%>
<!DOCTYPE html>
<html>
	<commonTag:head lessFile="/admin/asset/less/main.less">
		<%@ include file="../public/head.jsp" %>
	</commonTag:head>
    <body class="pageEditor">
    	<prototypeUI:nav />
    	
    	<commonUI:htmlEditor name="pageContent" isSimple="true" basePath="${basePath}" />
    	<div class="btns">
    		<button class="backBtn">返回</button>
    		<button class="saveBtn">保存</button>
	    	<button class="perviewBtn">预览</button>
    	</div>
		<% String content = ajaxjs.admin.PageEditor.PageEditor.read_jsp_fileContent(request);
		
		// 是否不允许编辑的？
		if(content == null){
		%>
		<script>
			alert("该页面没有任何编辑内容！");
			history.back();
		</script>
		<%} %>
		<textarea class="jspContent hide">
			<%=content%>
		</textarea>    
    	<script>
	    	seajs.use('bigfoot/widget/htmlEditor', function(_htmlEditor){
	    		bf.htmlEditor = _htmlEditor; // 单例
	    		_htmlEditor.el = document.querySelector('.htmlEditor');
	    		_htmlEditor.init();
	    		_htmlEditor.setValue(document.querySelector('.jspContent').value); 
	    		// 返回
	    		document.querySelector('.backBtn').onclick = function(e){
	    			window.history.go(-1);
	    		}
	    		// 提交数据
	    		document.querySelector('.saveBtn').onclick = function(e){
	    			bf.data.post('editor.jsp', {
	    				url : '${param.url}',
	    				content : encodeURIComponent(bf.htmlEditor.getValue())
	    			}, function(json){
	    				if(json.isOk)alert('修改页面成功！');else alert(json.msg);
	    			});
	    		}
	    		
	    		// 提交数据
	    		document.querySelector('.perviewBtn').onclick = function(e){
	    			window.open('${pageContext.request.contextPath}${param.url}', '_blank');
	    		}
	    	});
    	</script>
    	<commonUI:uploader isShowCover="false" isInsert2article="true" />
    	<commonUI:public load="form_upload_iframe" />
    </body>
</html>